package com.zzyl.nursing.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zzyl.nursing.domain.FamilyMemberElder;
import com.zzyl.nursing.vo.FamilyMemberElderVo;
import com.zzyl.nursing.vo.MemberElderVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 客户老人关联Mapper接口
 * 
 * @author ruoyi
 * @date 2024-05-19
 */
@Mapper
public interface FamilyMemberElderMapper extends BaseMapper<FamilyMemberElder>
{
    /**
     * 查询客户老人关联
     * 
     * @param id 客户老人关联主键
     * @return 客户老人关联
     */
    public FamilyMemberElder selectFamilyMemberElderById(Long id);

    /**
     * 查询客户老人关联列表
     * 
     * @param familyMemberElder 客户老人关联
     * @return 客户老人关联集合
     */
    public List<FamilyMemberElder> selectFamilyMemberElderList(FamilyMemberElder familyMemberElder);

    /**
     * 新增客户老人关联
     * 
     * @param familyMemberElder 客户老人关联
     * @return 结果
     */
    public int insertFamilyMemberElder(FamilyMemberElder familyMemberElder);

    /**
     * 修改客户老人关联
     * 
     * @param familyMemberElder 客户老人关联
     * @return 结果
     */
    public int updateFamilyMemberElder(FamilyMemberElder familyMemberElder);

    /**
     * 删除客户老人关联
     * 
     * @param id 客户老人关联主键
     * @return 结果
     */
    public int deleteFamilyMemberElderById(Long id);

    /**
     * 批量删除客户老人关联
     * 
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteFamilyMemberElderByIds(Long[] ids);

    @Select("SELECT fme.id, fme.family_member_id as familyMemberId, fme.elder_id as elderId, e.name as elderName " +
            "FROM family_member_elder fme LEFT JOIN elder e ON fme.elder_id = e.id " +
            "WHERE fme.family_member_id = #{memberId}")
    List<FamilyMemberElderVo> selectByMemberId(Long memberId);

    @Select("SELECT fme.id as mid, fme.remark as mremark, e.id as elderId, e.name, e.image, " +
            "e.bed_number as bedNumber, r.type_name as typeName, d.iot_id as iotId, " +
            "d.device_name as deviceName, d.product_key as productKey " +
            "FROM family_member_elder fme " +
            "LEFT JOIN elder e ON fme.elder_id = e.id " +
            "LEFT JOIN bed b ON e.bed_id = b.id " +
            "LEFT JOIN room r ON b.room_id = r.id " +
            "LEFT JOIN device d ON d.binding_location = e.id AND d.location_type = 0 " +
            "WHERE fme.family_member_id = #{memberId}")
    List<MemberElderVo> listByPage(Long memberId);
}
